$(function(){ 
	$.ajax({
		url:URL.FRAMEWORK+"/dictionary/queryByCodes?codes=web_skin_colors",
		success:function(res){
			initSkinColors(res);
		}	
	})  
	$("#skin_colors").on("click","li.skin_color",function(){ 
		var color = $(this).css('background-color');   
		changeTop(color);
		changeLeft(color);
		changeRight(color); 
		$("#skin_colors li.active-skin").toggleClass("active-skin");
		$(this).toggleClass("active-skin");
	}) 
})


function initSkinColors(response){ 
	var colors = null;
	if(!response || !response.data){
		colors = ['[222, 171, 127]','[37, 127, 203]'];
	}else{
		colors = [];
	}
	var dicts = response.data;
	var colorsHtml = "";
	for(var i = 0 ; i < dicts.length ; i++){
		colors.push(JSON.parse(dicts[i].value)) ;
		colorsHtml+= "<li class='skin_color' style='background-color: rgb("+colors[i].join(",")+");'></li>";
	}
	 
	//默认肤色  
	//var defalultColor = "rgb("+colors[0].join(",")+")";
	//$("#default_skin_color").css("background-color",defalultColor); 
	//肤色列表
	$("#skin_colors").html(colorsHtml);
	//默认肤色
	$("#skin_colors").find("li:first").toggleClass("active-skin");
}


function changeTop(color){
 
var top = $("#top_iframe",window.top.document).contents();
top.find("body").css("background-color",color);
}

function changeLeft(color){   
	var left = $("#left_iframe",window.top.document).contents(); 
	var rgb = color.replace(/(rgb\()|(\))/g,'').split(","); 
	left.find(".topbg").css("background-color",color);  
	rgb[0] = Number(rgb[0]);
	rgb[1] = Number(rgb[1]);
	rgb[2] = Number(rgb[2]);   
	rgb[0] = rgb[0] +(255-rgb[0])/1.2;
	rgb[1] = rgb[1] +(255-rgb[1])/1.2;
	rgb[2] = rgb[2] +(255-rgb[2])/1.2; 
	left.find("ul>li>div.select").css("background-color","rgb("+rgb.join(",")+")");    
}

function changeRight(color){   
	var right = $("#rigth_iframe",window.top.document).contents(); 
	var rgb = color.replace(/(rgb\()|(\))/g,'').split(","); 
	rgb[0] = Number(rgb[0]);
	rgb[1] = Number(rgb[1]);
	rgb[2] = Number(rgb[2]);   
	rgb[0] = rgb[0] +(255-rgb[0])/1.2;
	rgb[1] = rgb[1] +(255-rgb[1])/1.2;
	rgb[2] = rgb[2] +(255-rgb[2])/1.2; 
	right.find(".tabselect").css("background-color","rgb("+rgb.join(",")+")"); 
}